/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pub.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pub.entity.AddressInfo;
import com.thinkgem.jeesite.modules.pub.service.AddressInfoService;

/**
 * 企业地址信息Controller
 * @author wzl
 * @version 2018-10-12
 */
@Controller
@RequestMapping(value = "${adminPath}/pub/addressInfo")
public class AddressInfoController extends BaseController {

	@Autowired
	private AddressInfoService addressInfoService;
	
	@ModelAttribute
	public AddressInfo get(@RequestParam(required=false) String id) {
		AddressInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = addressInfoService.get(id);
		}
		if (entity == null){
			entity = new AddressInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("pub:addressInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(AddressInfo addressInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AddressInfo> page = addressInfoService.findPage(new Page<AddressInfo>(request, response), addressInfo); 
		model.addAttribute("page", page);
		return "modules/pub/addressInfoList";
	}

	@RequiresPermissions("pub:addressInfo:view")
	@RequestMapping(value = "form")
	public String form(AddressInfo addressInfo, Model model) {
		model.addAttribute("addressInfo", addressInfo);
		return "modules/pub/addressInfoForm";
	}

	@RequiresPermissions("pub:addressInfo:edit")
	@RequestMapping(value = "save")
	public String save(AddressInfo addressInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, addressInfo)){
			return form(addressInfo, model);
		}
		addressInfoService.save(addressInfo);
		addMessage(redirectAttributes, "保存企业地址信息成功");
		return "redirect:"+Global.getAdminPath()+"/pub/addressInfo/?repage";
	}
	
	@RequiresPermissions("pub:addressInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(AddressInfo addressInfo, RedirectAttributes redirectAttributes) {
		addressInfoService.delete(addressInfo);
		addMessage(redirectAttributes, "删除企业地址信息成功");
		return "redirect:"+Global.getAdminPath()+"/pub/addressInfo/?repage";
	}

}